<template>
    <div class="permission-list-container">
        <!-- 页面标题区域 -->
        <div class="page-header">
            <h1 class="page-title">权限管理</h1>
            <p class="page-subtitle">管理系统中的所有权限配置</p>
        </div>
        <!-- 主要内容卡片 -->
        <el-card class="permission-card">
            <!-- 搜索区域 -->
            <div class="search-area">
                <el-form :inline="true" class="search-form">
                    <el-form-item label="权限名称" label-width="90px">
                        <el-input placeholder="请输入权限名称进行搜索" v-model="FindData.PermissionName" :prefix-icon="Search"
                            class="search-input" clearable />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="GetPermissions()" class="search-button" :loading="isLoading">
                            查询
                        </el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="add-button" :loading="isLoading" @click="OpenAddway()">
                            <el-icon>
                                <Plus />
                            </el-icon>
                            添加权限
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
            <!-- 数据表格 -->
            <div class="table-area">
                <el-table :data="permissionData" style="width: 100%" class="permission-table" border stripe
                    highlight-current-row :loading="isLoading" empty-text="暂无权限数据"
                    empty-image="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 120 120' width='120' height='120'%3E%3Cpath fill='%23E5E6EB' d='M30,30 L90,30 L90,90 L30,90 Z'/%3E%3Cpath fill='%23C0C4CC' d='M40,40 L80,40 L80,80 L40,80 Z'/%3E%3Cpath fill='%23909399' d='M50,50 L70,50 L70,70 L50,70 Z'/%3E%3C/svg%3E">
                    <el-table-column prop="Id" label="权限编号" width="100" align="center" sortable />
                    <el-table-column prop="PermissionName" label="权限名称" width="200" align="center"
                        show-overflow-tooltip />
                    <el-table-column prop="PermissionWay" label="权限URL" width="250" align="center"
                        show-overflow-tooltip />
                    <el-table-column type="index" label="序号" width="80" align="center" fixed="left" />
                    <el-table-column label="操作" width="200" align="center" fixed="right">
                        <template v-slot="scope">
                            <el-button type="primary" size="small" class="edit-button" @click="OpenUpdateway(scope.row)"
                                style="margin-right: 10px">
                                <el-icon>
                                    <Edit />
                                </el-icon>
                                编辑
                            </el-button>
                            <el-button type="danger" size="small" @click="RoleDelway(scope.row)" class="delete-button">
                                <el-icon>
                                    <Delete />
                                </el-icon>
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 权限添加对话框 -->
                <el-dialog v-model="OpenPermissionsAdd" title="权限新增" width="500">
                    {{ AddData }}
                    <el-form :model="AddData" label-width="auto" style="max-width: 600px">
                        <el-form-item label="权限名称">
                            <el-input v-model="AddData.PermissionName" />
                        </el-form-item>
                        <el-form-item label="权限URL">
                            <el-input v-model="AddData.PermissionWay" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="PermissionAddway()">添加</el-button>
                            <el-button @click="OpenPermissionsAdd = false">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
                <!-- 权限修改对话框 -->
                <el-dialog v-model="OpenPermissionsUpdate" title="权限编辑" width="500">
                    {{ AddData }}
                    <el-form :model="AddData" label-width="auto" style="max-width: 600px">
                        <el-form-item label="权限名称">
                            <el-input v-model="AddData.PermissionName" />
                        </el-form-item>
                        <el-form-item label="权限URL">
                            <el-input v-model="AddData.PermissionWay" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="PermissionUpdateway()">保存</el-button>
                            <el-button @click="OpenPermissionsUpdate = false">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
            </div>
        </el-card>
    </div>
</template>
<script setup lang="ts">
import httpaxios from '@/AxiosHelp';
import { Search, Plus, Edit, Delete } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
// 加载状态
const isLoading = ref(false);
onMounted(() => {
    GetPermissions(); //获取权限信息
})
//查询参数
const FindData = reactive({
    PermissionName: ""
})
//显示查询结果权限数据
const permissionData = ref([])
// 权限添加对话框状态
const OpenPermissionsAdd = ref(false);
// 权限修改对话框状态
const OpenPermissionsUpdate = ref(false);
//添加修改用的参数变量
const AddData = reactive({
    "Id": null,
    "PermissionName": "",
    "PermissionWay": ""
})

//#region 权限显示部分
const GetPermissions = () => {
    isLoading.value = true;
    httpaxios.get("/api/Permissions/PermissionGet", {
        params: {
            PermissionName: FindData.PermissionName
        }
    })
        .then(res => {
            console.log(res.data);
            permissionData.value = res.data.Data || [];
        })
        .catch(error => {
            console.error('获取权限数据失败:', error);
            ElMessage.error('获取权限数据失败，请稍后重试');
        })
        .finally(() => {
            isLoading.value = false;
        })
}
//#endregion

//#region 删除显示部分
const RoleDelway = (row: any) => {
    ElMessageBox.confirm(
        '确认要删除该权限吗？此操作不可恢复。',
        '删除确认',
        {
            confirmButtonText: '确认删除',
            cancelButtonText: '取消',
            type: 'warning',
            center: true,
            confirmButtonClass: 'el-button--danger'
        }
    ).then(() => {
        isLoading.value = true;
        httpaxios.put("/api/Permissions/PermissionDel", { Id: row.Id })
            .then(res => {
                console.log(res.data);
                if (res.data.Code == 200) {
                    ElMessage.success(res.data.Message || '删除成功')
                    //刷新页面
                    GetPermissions();
                }
                else {
                    ElMessage.error(res.data.Message || '删除失败')
                }
            })
            .catch(error => {
                console.error('删除权限失败:', error);
                ElMessage.error('删除权限失败，请稍后重试');
            })
            .finally(() => {
                isLoading.value = false;
            })
    })
}
//#endregion

//#region 添加显示部分
//打开添加
const OpenAddway = () => {
    //打开对话框
    OpenPermissionsAdd.value = true;
    //清空
    AddData.Id = null,
        AddData.PermissionName = "",
        AddData.PermissionWay = ""
}
//添加
const PermissionAddway = () => {
    httpaxios.post("/api/Permissions/PermissionAdd", AddData).then(res => {
        console.log(res.data);
        if (res.data.Code == 200) {
            ElMessage.success(res.data.Message)
            OpenPermissionsAdd.value = false;
            GetPermissions();  //刷新页面
        }
        else {
            ElMessage.error(res.data.Message)
        }
    })
}
//#endregion

//#region 编辑显示部分
//打开编辑
const OpenUpdateway = (row: any) => {
    //打开对话框
    OpenPermissionsUpdate.value = true;
    //反填
    AddData.Id = row.Id,
        AddData.PermissionName = row.PermissionName,
        AddData.PermissionWay = row.PermissionWay
}
//权限修改
const PermissionUpdateway = () => {
    httpaxios.put("/api/Permissions/PermissionUpdate", AddData).then(res => {
        console.log(res.data);
        if (res.data.Code == 200) {
            ElMessage.success(res.data.Message)
            OpenPermissionsUpdate.value = false;
            GetPermissions();  //刷新页面
        }
        else {
            ElMessage.error(res.data.Message)
        }
    })
}
//#endregion

</script>
<style scoped>
/* 主容器样式 */
.permission-list-container {
    padding: 24px;
    background-color: #f5f7fa;
    min-height: 100vh;
    transition: all 0.3s ease;
}

/* 页面标题样式 */
.page-header {
    margin-bottom: 24px;
}

.page-title {
    font-size: 24px;
    font-weight: 600;
    color: #303133;
    margin: 0 0 8px 0;
}

.page-subtitle {
    font-size: 14px;
    color: #909399;
    margin: 0;
}

/* 卡片样式 */
.permission-card {
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    border: none;
    overflow: hidden;
    background-color: #fff;
    transition: all 0.3s ease;
}

/* 搜索区域样式 */
.search-area {
    padding: 20px 24px;
    background-color: #fafafa;
    border-bottom: 1px solid #ebeef5;
}

.search-form {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
}

.search-input {
    width: 320px;
    min-width: 200px;
    transition: all 0.3s ease;
}

/* 按钮样式统一 */
.search-button,
.add-button {
    padding: 8px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.add-button {
    background-color: #f0f2f5;
    border-color: #e4e7ed;
    color: #606266;
}

.add-button:hover {
    background-color: #e6f7ff;
    border-color: #91d5ff;
    color: #1890ff;
}

/* 表格区域样式 */
.table-area {
    padding: 24px;
}

.permission-table {
    border-radius: 8px;
    overflow: hidden;
}

/* 表格自定义样式 */
:deep(.el-table) {
    --el-table-border: 1px solid #ebeef5;
    --el-table-border-color: #ebeef5;
    font-size: 14px;
    transition: all 0.3s ease;
}

/* 表格头部样式 */
:deep(.el-table th) {
    background-color: #fafafa !important;
    font-weight: 600 !important;
    color: #606266 !important;
    border-bottom: 1px solid #ebeef5 !important;
    padding: 12px 0 !important;
}

/* 表格行样式 */
:deep(.el-table td) {
    padding: 12px 0 !important;
    border-bottom: 1px solid #ebeef5 !important;
}

/* 表格行悬停效果 */
:deep(.el-table__row:hover > td) {
    background-color: #f5f7fa !important;
}

/* 表格选中行样式 */
:deep(.el-table__row.el-table__row--current > td) {
    background-color: #e6f7ff !important;
}

/* 表格空状态样式 */
:deep(.el-table__empty-block) {
    padding: 80px 0 !important;
}

:deep(.el-table__empty-text) {
    color: #909399 !important;
    font-size: 14px !important;
}

/* 操作按钮样式 */
:deep(.el-button) {
    transition: all 0.3s ease;
    border-radius: 4px;
}

:deep(.el-button--small) {
    padding: 5px 12px;
    font-size: 12px;
}

/* 加载状态样式 */
:deep(.el-loading-mask) {
    background-color: rgba(255, 255, 255, 0.9) !important;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
    .permission-list-container {
        padding: 16px;
    }

    .page-title {
        font-size: 20px;
    }

    .search-input {
        width: 100%;
        margin-bottom: 10px;
    }

    .search-form {
        justify-content: flex-start;
    }

    .table-area {
        padding: 16px;
    }

    :deep(.el-table__fixed) {
        display: none !important;
    }

    :deep(.el-table__fixed-right) {
        display: none !important;
    }
}

@media screen and (max-width: 480px) {
    .permission-list-container {
        padding: 12px;
    }

    .search-area {
        padding: 16px;
    }
}
</style>